//
//  InSoundHeaderView.m
//  Inner  Voice
//
//  Created by 徐海宇 on 16/3/3.
//  Copyright © 2016年 徐海宇. All rights reserved.
//

#import "InSoundHeaderView.h"

#import "UIImageView+WebCache.h"

#import "InSoundFavouriteTool.h"

#import "InSoundDetail.h"

#import "XCHudHelper.h"


#import "InSoundTableViewController.h"

@interface InSoundHeaderView ()

@end


@implementation InSoundHeaderView


- (instancetype)init {
    self = [super init];
    
    if (self) {
       NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"InSoundHeaderView" owner:nil options:nil];
        self = array.lastObject;
    }
    return self;
}

- (void)loadImageWithStr:(NSString*)str WithImageView:(UIImageView *)imageView{
    
    NSURL *url = [NSURL URLWithString:str];
    
    [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    
}

- (void)setSoundDetail:(InSoundDetail *)soundDetail {
#pragma mark - 加载图片加载图片加载图片加载图片加载图片加载图片加载图片加载图片加载图片加载图片加载图片
//    加载图片
    
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    [self loadImageWithStr:soundDetail.sound_creater_icon WithButton:self.UserHeader];
    
    [self loadImageWithStr:soundDetail.sound_picture WithImageView:self.soundImageView];
    //    ok
    [self.UserHeader setUserInteractionEnabled:NO];
    self.userName.text = soundDetail.sound_channel_name;
//    [self.userName setTitle:soundDetail.sound_channel_name forState:UIControlStateNormal];
    self.soundNameLabel.text = soundDetail.sound_name;
    
    self.concerCountLabel.text = soundDetail.sound_comment_count;
    
    self.creater_id = soundDetail.sound_creater_id;
    
    BOOL success = [InSoundFavouriteTool selectFriend:self.creater_id WithUser_id:user_id];
    
    if (success) {
        self.concerBtn.selected = YES;
    }

}

- (IBAction)concernBtnClick:(id)sender {
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    if (user_id == nil) {
        [self.delegate concernBtn];
    }else {
        if (self.concerBtn.selected == NO) {
            self.concerBtn.selected = YES;
            self.concerBtn.enabled = YES;
            [self concernSoundCreater];
        }
    
    }
    
    
}


- (void)concernSoundCreater {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    AFHTTPSessionManager *httpSessionManager = [AFHTTPSessionManager manager];
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSString *user_id = [userDefault valueForKey:@"u_id"];
    
    NSString *url = [NSString stringWithFormat:@"http://1.wangtest11.applinzi.com/home/Banner/userFriend?user_id=%@&friend_id=%@",user_id,self.creater_id];

    [httpSessionManager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [XCHudHelper showMessage:@"关注成功"];
        
        [InSoundFavouriteTool insertUser:self.creater_id WithUser_id:user_id];
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [XCHudHelper showMessage:@"关注失败，我们会尽快解决该问题"];
        self.concerBtn.selected = NO;
        self.concerBtn.enabled = YES;
    }];
    }else {
        [self showAlertView:@"当前网络不可用"];
    }

}
#pragma mark - 封装UIAlertView方法，直接调用就可以显示提示
-(void)showAlertView:(NSString *)string {
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:string delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alertView show];
    
}

- (void)loadImageWithStr:(NSString *)str WithButton:(UIButton *)button {
    if ([AppDelegate shareAppdelegate].canConnectedToTheInternet) {

    NSURL *url = [NSURL URLWithString:str];
    
    //根据图片的url下载图片数据
    dispatch_queue_t queue = dispatch_queue_create("loadImage", NULL);//创建GCD线程队列
    dispatch_async(queue, ^{
        //异步下载图片
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        //主线程刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            [button setBackgroundImage:image forState:UIControlStateNormal];
        });
    });
    }else {
        [self showAlertView:@"当前网络不可用"];
    }
    
}


-(void)dealloc{

  
}




@end
